package com.ccdt.tv.smartrecommend.view.web;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.ccdt.tv.smartrecommend.Constant;
import com.ccdt.tv.smartrecommend.FromLiveBean;
import com.ccdt.tv.smartrecommend.model.http.Config;
import com.ccdt.tv.smartrecommend.view.widget.layout.FloatLayout;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by sheng on 2018/9/16.
 * 智能推荐浏览器
 */

public class SRWebView extends WebView {

    private FloatLayout mView;

    public SRWebView(FloatLayout view) {
        super(Utils.getApp());
        mView = view;
        init();
        initWebSetting();
    }

    private void init() {
        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
        setLayoutParams(params);
        setBackgroundColor(Color.TRANSPARENT);

        setVisibility(View.VISIBLE);
        requestFocus();
        addJavascriptInterface(new SRJsInterface(mView), "android");

        setWebViewClient(new SRWebViewClient());
        setBackgroundColor(0);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebSetting() {
        WebSettings webSettings = getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);   //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true);  // 缩放至屏幕的大小
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//根据cache-control决定是否从网络上取数据。
//        Log.w("syt", "initWebSetting: net:" + NetworkUtils.isConnected());
//        if (NetworkUtils.isConnected()) {
//        } else {
//            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没网，则从本地获取，即离线加载
//        }
//        webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
//        webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
//        webSettings.setAppCacheEnabled(true);   //开启 Application Caches 功能
//        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
//        Log.w("syt", "initWebSetting: path:" + Constant.CACHE_DIR_PATH);
//        webSettings.setAppCachePath(Constant.CACHE_DIR_PATH); //设置  Application Caches 缓存目录
//        webSettings.setAppCacheMaxSize(20 * 1024 * 1024);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);//HTTPS，注意这个是在LOLLIPOP以上才调用的
//        }
    }

    /**
     * 加载url
     */
    @Override
    public void loadUrl(String url) {
        if (!TextUtils.isEmpty(url)) {
            super.loadUrl(url);
        } else {
            String webUrl = Config.URL_WEB_INDEX;
            String liveJson = SPUtils.getInstance(Constant.SP_NAME).getString(Constant.SP_LIVE_JSON);
            if (TextUtils.isEmpty(liveJson)) {
                super.loadUrl(webUrl);
            } else {
                FromLiveBean bean = new Gson().fromJson(liveJson, FromLiveBean.class);
                try {
                    webUrl = webUrl + "?serviceId="
                            + Integer.parseInt(bean.getSid()) + "&juName="
                            + URLEncoder.encode(bean.getProgname(), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                Log.w("syt", "loadUrl mWebViewUrl = " + webUrl);
                super.loadUrl(webUrl);
            }
        }
    }
}
